﻿using DevExpress.Utils.MVVM;
using DevExpress.Utils.MVVM.Services;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevUIUtils.MVVM.Views
{
    /// <summary>
    /// DevMVVM视图服务注册帮助类
    /// </summary>
    public static class ViewServicesRegHelper
    {
        /// <summary>
        /// 注册自定义对话框窗体服务
        /// </summary>
        /// <param name="context">MVVM上下文对象</param>
        /// <param name="bCloseBtn">是否显示关闭宾馆</param>
        /// <param name="queryView">窗体查找委托，当跨程序集查询窗体时或无法正确找到窗体时可以在这里强行指定</param>
        /// <param name="borderStyle">窗体样式，FormBorderStyle</param>
        /// <param name="key">对话框Key</param>
        public static void RegDialogFormService(this MVVMContext context, bool bCloseBtn = true, QueryEventHandler<QueryViewEventArgs, Control> queryView = null, FormBorderStyle borderStyle = FormBorderStyle.FixedSingle, string key = "Dialog")
        {
            var dialog = WindowService.CreateXtraFormService(context.ContainerControl);
            if (queryView != null) dialog.QueryView += queryView;
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowMode = WindowService.WindowShowMode.Modal;
            dialog.WindowStyle = window =>
            {
                XtraForm frm = (XtraForm)window;
                frm.ShowInTaskbar = false;
                frm.MaximizeBox = false;
                frm.MinimizeBox = false;
                frm.CloseBox = bCloseBtn;
                frm.FormBorderStyle = borderStyle;
            };
            context.RegisterService(key, dialog);
        }


        //UI层需要以下设置
        //htmlPopu.ViewModelType = typeof(VM_Dialog1);
        //    htmlPopu.ViewModelSet += (s, e) =>
        //    {
        //        var dlgFluent = htmlPopu.OfType<VM_Dialog1>();
        //dlgFluent.BindCommand("btnClose", x => x.CloseDialog);
        //        dlgFluent.BindCommand("btnAccept", x => x.AcceptDialog);
        //    };
        public static void RegHtmlPopupDialog(this MVVMContext context, HtmlContentPopup popup, bool bCloseBtn = true, FormBorderStyle borderStyle = FormBorderStyle.FixedSingle, string key = "HtmlDialog")
        {
            var dialog = popup.CreateWindowService(popup.ContainerControl);
            dialog.ShowMode = WindowService.WindowShowMode.Modal;
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.WindowStyle = window =>
            {
                var dlgWindow = window as IPopupWindow;
                dlgWindow.PopupSize = new Size(332, 232);
                dlgWindow.DestroyOnHide = false;
            };
            context.RegisterService(key, dialog);
        }

        /// <summary>
        /// 注册窗体服务
        /// </summary>
        /// <param name="context">MVVM上下文对象</param>
        /// <param name="queryView">窗体查找委托，当跨程序集查询窗体时或无法正确找到窗体时可以在这里强行指定</param>
        public static void RegGeneralFormService(this MVVMContext context, QueryEventHandler<QueryViewEventArgs, Control> queryView = null)
        {
            var win = WindowService.CreateXtraFormService(context.ContainerControl);
            if (queryView != null) win.QueryView += queryView;
            win.StartPosition = FormStartPosition.CenterParent;
            context.RegisterService("Window", win);
        }

        /// <summary>
        /// 注册自定义文件打开对话框
        /// </summary>
        /// <param name="context">MVVM上下文对象</param>
        /// <param name="title">对话框标题,默认"打开"</param>
        /// <param name="filter">打开文件过滤字符串，默认""</param>
        public static void RegCustomOpenFileDialog(this MVVMContext context, string title = "打开", string filter = "")
        {
            var multiple = new OpenFileDialog() { Multiselect = true };
            multiple.Title = title;
            multiple.Filter = filter;
            context.RegisterService("MultiselectOpenFileDialog", OpenFileDialogService.Create(multiple));
            var single = new OpenFileDialog();
            single.Title = title;
            single.Filter = filter;
            context.RegisterService("SingleOpenFileDialog", OpenFileDialogService.Create(single));
        }

        /// <summary>
        /// 注册带关键字的自定义文件打开对话框(多选)
        /// </summary>
        /// <param name="context">MVVM上下文对象</param>
        /// <param name="key">文件打开对话框关键字</param>
        /// <param name="title">对话框标题,默认"打开"</param>
        /// <param name="filter">打开文件过滤字符串，默认""</param>
        public static void RegCustomMultipleOpenFileDialog(this MVVMContext context, string key = "", string title = "打开", string filter = "", string initialDirectory = "")
        {
            var multiple = new OpenFileDialog() { Multiselect = true };
            multiple.Title = title;
            multiple.Filter = filter;
            multiple.InitialDirectory = initialDirectory;
            context.RegisterService(key, OpenFileDialogService.Create(multiple));
        }

        /// <summary>
        /// 注册带关键字的自定义文件打开对话框(单选)
        /// </summary>
        /// <param name="context">MVVM上下文对象</param>
        /// <param name="key">文件打开对话框关键字</param>
        /// <param name="title">对话框标题,默认"打开"</param>
        /// <param name="filter">打开文件过滤字符串，默认""</param>
        public static void RegCustomSingleOpenFileDialog(this MVVMContext context, string key = "", string title = "打开", string filter = "")
        {
            var single = new OpenFileDialog();
            single.Title = title;
            single.Filter = filter;
            context.RegisterService(key, OpenFileDialogService.Create(single));
        }

    }
}
